﻿using System.Web.Mvc;
using WebUI.Areas.Bots.Models;
using System.Linq;
using System.Collections.Generic;

namespace WebUI.Areas.Bots.Controllers
{
    public partial class NavController : Controller
    {
        #region Member Variables
        
        readonly ISessionHelper _sessionHelper; 

        #endregion

        #region Public Properties
        
        public IList<string> AllSteps
        {
            get
            {
                return new string[7] 
                { 
                    MVC.Bots.General.Name,
                    MVC.Bots.BotType.Name,
                    MVC.Bots.Schedule.Name,
                    MVC.Bots.BotFilters.Name,
                    MVC.Bots.Operations.Name,
                    MVC.Bots.UsersAndMachines.Name,
                    MVC.Bots.Notifications.Name
                };
            }
        } 

        #endregion

        #region Constructor

        public NavController(ISessionHelper sessionHelper)
        {
            this._sessionHelper = sessionHelper;
        } 

        #endregion

        #region Public Actions

        [ChildActionOnly]
        public virtual PartialViewResult Menu(string step = "General")
        {
            if (step == null)/*check for null because In case of validation error View(name,Model) is returned instead of RedirectToRoute*/
            {
                step = _sessionHelper.CurrentStep;
            }
            else
            {
                _sessionHelper.CurrentStep = step;
            }

            int currentStepIdx = AllSteps.IndexOf(step);

            if (currentStepIdx > _sessionHelper.MaxStepReached)
            {
                _sessionHelper.MaxStepReached = currentStepIdx;
            }

            var model = new NavModel
            {
                SelectedStep = step,
                Steps = GenerateWizardSteps(isBotSaved: _sessionHelper.Bot.BotId != 0,
           maxStepReached: _sessionHelper.MaxStepReached)
            };

            return PartialView(model);
        } 

        #endregion

        #region Private Methods
        
        private IEnumerable<WizardStep> GenerateWizardSteps(bool isBotSaved, int maxStepReached)
        {
            for (int i = 0; i < AllSteps.Count; i++)
            {
                yield return new WizardStep
                {
                    Name = AllSteps[i],
                    Enabled = isBotSaved || i <= maxStepReached
                };
            }
        } 

        #endregion
    }
}